package org.utils
{
	import mx.collections.ArrayCollection;
	import mx.utils.URLUtil;

	public class Utils
	{
		public function Utils()
		{
		}
		
		public static function getXMLtracklist(item:Object):XML
		{
			var tracklist:XML = <tracklist />;
			var i:int = 0;
			
			var children:ArrayCollection = new ArrayCollection();
			var trackCollection:ArrayCollection = new ArrayCollection();
			children.addItem(item);
			browseChildren(children, trackCollection);
			fillTracklist(tracklist, trackCollection);
			return tracklist;
		}
		
		private static function browseChildren(children:Object, trackCollection:ArrayCollection):void
		{
			if(children != null && children.length > 0){
				for each(var item:Object in children){
					if(parseInt(item.dir) == 1){
						browseChildren(item.children, trackCollection);		
					}else{
						trackCollection.addItem(item);
					}
				}
				
			}
		}
		
		private static function fillTracklist(tracklist:XML, trackCollection:ArrayCollection):void
		{
			for(var i:int = 0; i< trackCollection.length; i++){
				tracklist.track[i] = <track id={trackCollection[i].id} />;
			}
		}
		
		public static function getBaseURL(url:String):String
		{
			if (url == null){
				return null;
			}
			url = url.replace("Jukebox.html", "").replace("Jukebox.swf", "");
			return url;
		}
		
		public static function formatTwoDigits(number:int):String
		{
			if(number < 10){
				return "0"+number;
			}else{
				return ""+number;
			}
		}
		
		public static function formatTime(seconds:int):String
		{
			return formatTwoDigits(int(seconds / 60)) + ":" + formatTwoDigits(seconds % 60);
		}
	}
}